//
// Created by HP on 2023/12/30.
//

#include "lcd_driver.h"



LCD_Driver::LCD_Driver(){

}

void LCD_Driver::Init(uint8_t USE_HORIZONTAL) {
    LCD_CS_H;
    LCD_CMD;
    HORIZONTAL = USE_HORIZONTAL;
    if( HORIZONTAL == 0 || HORIZONTAL == 1){
        lcd_w = 172;
        lcd_h = 320;
    }
    else{
        lcd_w = 320;
        lcd_h = 172;
    }

    HAL_GPIO_WritePin(LCD_RESET_GPIO_Port,LCD_RESET_Pin,GPIO_PIN_RESET);
    HAL_Delay(30);
    HAL_GPIO_WritePin(LCD_RESET_GPIO_Port,LCD_RESET_Pin,GPIO_PIN_SET);
    HAL_Delay(100);
    HAL_GPIO_WritePin(LCD_BLK_GPIO_Port,LCD_BLK_Pin,GPIO_PIN_RESET);


    _lcd_cmd(0x11);
    _lcd_cmd(0x36);
    if(USE_HORIZONTAL==0)_lcd_data(0x00);
    else if(USE_HORIZONTAL==1)_lcd_data(0xC0);
    else if(USE_HORIZONTAL==2)_lcd_data(0x70);
    else _lcd_data(0xA0);

    _lcd_cmd(0x3A);
    _lcd_data(0x05);

    _lcd_cmd(0xB2);
    _lcd_data(0x0C);
    _lcd_data(0x0C);
    _lcd_data(0x00);
    _lcd_data(0x33);
    _lcd_data(0x33);

    _lcd_cmd(0xB7);
    _lcd_data(0x35);

    _lcd_cmd(0xBB);
    _lcd_data(0x35);

    _lcd_cmd(0xC0);
    _lcd_data(0x2C);

    _lcd_cmd(0xC2);
    _lcd_data(0x01);

    _lcd_cmd(0xC3);
    _lcd_data(0x13);

    _lcd_cmd(0xC4);
    _lcd_data(0x20);

    _lcd_cmd(0xC6);
    _lcd_data(0x0F);

    _lcd_cmd(0xD0);
    _lcd_data(0xA4);
    _lcd_data(0xA1);

    _lcd_cmd(0xD6);
    _lcd_data(0xA1);

    _lcd_cmd(0xE0);
    _lcd_data(0xF0);
    _lcd_data(0x00);
    _lcd_data(0x04);
    _lcd_data(0x04);
    _lcd_data(0x04);
    _lcd_data(0x05);
    _lcd_data(0x29);
    _lcd_data(0x33);
    _lcd_data(0x3E);
    _lcd_data(0x38);
    _lcd_data(0x12);
    _lcd_data(0x12);
    _lcd_data(0x28);
    _lcd_data(0x30);

    _lcd_cmd(0xE1);
    _lcd_data(0xF0);
    _lcd_data(0x07);
    _lcd_data(0x0A);
    _lcd_data(0x0D);
    _lcd_data(0x0B);
    _lcd_data(0x07);
    _lcd_data(0x28);
    _lcd_data(0x33);
    _lcd_data(0x3E);
    _lcd_data(0x36);
    _lcd_data(0x14);
    _lcd_data(0x14);
    _lcd_data(0x29);
    _lcd_data(0x32);

// 	_lcd_cmd(0x2A);
//	__lcd_data(0x00);
//	__lcd_data(0x22);
//	__lcd_data(0x00);
//	__lcd_data(0xCD);
//	__lcd_data(0x2B);
//	__lcd_data(0x00);
//	__lcd_data(0x00);
//	__lcd_data(0x01);
//	__lcd_data(0x3F);
//	_lcd_cmd(0x2C);
    _lcd_cmd(0x21);

    _lcd_cmd(0x11);
    HAL_Delay(120);
    _lcd_cmd(0x29);

}



void LCD_Driver::_spi_send(uint8_t *data, uint16_t len) {
    LCD_CS_L;
    HAL_SPI_Transmit(&hspi1,data,len,0xffff);
    LCD_CS_H;
}
void LCD_Driver::_spi_sendDMA(uint8_t *data,uint16_t len){
    LCD_CS_L;
    HAL_SPI_Transmit_DMA(&hspi1,data,len);
    while(HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
    LCD_CS_H;
}
void LCD_Driver::_lcd_cmd(uint8_t cmd){
    LCD_CMD;
    _spi_send(&cmd,1);
}
void LCD_Driver::_lcd_data(uint8_t data){
    LCD_DATA;
    _spi_send(&data,1);
}
void LCD_Driver::_lcd_data16(uint16_t data){
    _lcd_data(data >> 8);
    _lcd_data(data);
}
void LCD_Driver::_lcd_datas(uint8_t *data,uint16_t len){
    LCD_DATA;
    _spi_send(data,len);
}
void LCD_Driver::_lcd_datasDMA(uint8_t *data,uint16_t len){
    LCD_DATA;
    _spi_sendDMA(data,len);
}

void LCD_Driver::_lcd_set_address(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2){
    if(HORIZONTAL==0)
    {
        _lcd_cmd(0x2a);//�е�ַ����
        _lcd_data16(x1+34);
        _lcd_data16(x2+34);
        _lcd_cmd(0x2b);//�е�ַ����
        _lcd_data16(y1);
        _lcd_data16(y2);
        _lcd_cmd(0x2c);//������д
    }
    else if(HORIZONTAL==1)
    {
        _lcd_cmd(0x2a);//�е�ַ����
        _lcd_data16(x1+34);
        _lcd_data16(x2+34);
        _lcd_cmd(0x2b);//�е�ַ����
        _lcd_data16(y1);
        _lcd_data16(y2);
        _lcd_cmd(0x2c);//������д
    }
    else if(HORIZONTAL==2)
    {
        _lcd_cmd(0x2a);//�е�ַ����
        _lcd_data16(x1);
        _lcd_data16(x2);
        _lcd_cmd(0x2b);//�е�ַ����
        _lcd_data16(y1+34);
        _lcd_data16(y2+34);
        _lcd_cmd(0x2c);//������д
    }
    else
    {
        _lcd_cmd(0x2a);//�е�ַ����
        _lcd_data16(x1);
        _lcd_data16(x2);
        _lcd_cmd(0x2b);//�е�ַ����
        _lcd_data16(y1+34);
        _lcd_data16(y2+34);
        _lcd_cmd(0x2c);//������д
    }
}

void LCD_Driver::fill(uint16_t color){
    uint32_t i;

    for(i=0;i<172*320 / 32;i++)
        lcd_buff[i] = color;
    _lcd_set_address(0,0,lcd_w - 1,lcd_h - 1);
    LCD_DATA;
    LCD_CS_L;
    for(i=0;i<32;i++){
        HAL_SPI_Transmit_DMA(&hspi1,(uint8_t *)lcd_buff,320*172/16);
        while(HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
    }
    LCD_CS_H;

}
//局部填充
void LCD_Driver::fillpart(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t color){
    uint32_t i;
    uint16_t w,h;
    w = x2 - x1 ;
    h = y2 - y1 ;
    uint16_t total = w * h;
    for(i=0;i< total;i++)
        lcd_buff[i] = color;
    _lcd_set_address(x1,y1,x2,y2);
    LCD_DATA;
    LCD_CS_L;
    HAL_SPI_Transmit_DMA(&hspi1,(uint8_t *)lcd_buff,total << 1);
    while(HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
    LCD_CS_H;
}
//void LCD_Driver::draw_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t *buff){
//    _lcd_set_address(x1,y1,x2 - 1,y2 - 1);
//    LCD_DATA;
//    LCD_CS_L;
//    HAL_SPI_Transmit_DMA(&hspi1,(uint8_t *)buff,(x2-x1)*(y2-y1)*2);
//    while(HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
//    LCD_CS_H;
//}
void LCD_Driver::draw_rectangle(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2){
    _lcd_set_address(x1,y1,x2 - 1,y2 - 1);
    LCD_DATA;
    LCD_CS_L;
    HAL_SPI_Transmit_DMA(&hspi1,(uint8_t *)lcd_buff,(x2-x1)*(y2-y1)*2);
    while(HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
    LCD_CS_H;
}
void LCD_Driver::draw_rectangle_Aim(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t *buff){
    _lcd_set_address(x1,y1,x2 - 1,y2 - 1);
    LCD_DATA;
    LCD_CS_L;
    HAL_SPI_Transmit_DMA(&hspi1,(uint8_t *)buff,(x2-x1)*(y2-y1)*2);
}

//
void LCD_Driver::draw_point(uint16_t x ,uint16_t y,uint16_t color){
    _lcd_set_address(x,y,x,y);
    LCD_DATA;
    LCD_CS_L;
    HAL_SPI_Transmit(&hspi1,(uint8_t *)&color,2,0xffff);
    LCD_CS_H;
}




















//
//void LCD_Driver::Color_bar_display(uint8_t len,uint16_t *buff){
//    uint16_t i,j;
//    uint16_t p = lcd_w / len;
//    //uint16_t color[5]= {0xe00a,0x0bcd,0xe234,0xed12,0xccdd};
//    for ( i = 0; i < lcd_h ; i++){
//        for ( j = 0; j < lcd_w ; j++) {
//            lcd_buff[lcd_w * i + j] = buff[ j / p];
//        }
//    }
//}
//void LCD_Driver::update(){
//    _lcd_set_address(0,0,lcd_w - 1,lcd_h - 1);
//    LCD_DATA;
//    LCD_CS_L;
//    HAL_SPI_Transmit_DMA(&hspi1,(uint8_t *)lcd_buff,45040);
//    while(HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
//    HAL_SPI_Transmit_DMA(&hspi1,(uint8_t *)(lcd_buff +22520),65040);
//    while(HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
//    LCD_CS_H;
//}
//
//void LCD_Driver::part_update(uint16_t y1,uint16_t y2){
//    uint16_t h = y2 - y1 - 1;
//    while(HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
//    _lcd_set_address(0,y1,lcd_w - 1,y2 - 1);
//    LCD_DATA;
//    LCD_CS_L;
//    HAL_SPI_Transmit_DMA(&hspi1,(uint8_t *)(lcd_buff + lcd_w*y1),lcd_w*h*2);
//    while(HAL_DMA_GetState(hspi1.hdmatx) != HAL_DMA_STATE_READY);
//    LCD_CS_H;
//}